var searchparam;
/**
 *model 
 */    
    Ext.define('model_ry', {
         extend: 'Ext.data.Model',
         fields: [
                {name: 'flogname',type: 'string'},
                {name: 'fusername',type: 'string'},
                {name: 'fpassword',type: 'string'},
                {name: 'fpower',type: 'string'},
                {name: 'swjg_dm',type: 'string'},
                {name: 'swjg_mc',type: 'string'}
         ]
     });
/**
 *store 
 */     
     var store_ry=Ext.create('Ext.data.Store', {
        model: 'model_ry',
        autoDestroy: true,
        autoLoad: true,
        pageSize:100,
        listeners:{
            beforeload:function(store,operation,eOpts){//添加事件在获取数据前触发
                if(searchparam){
                    store.proxy.extraParams=searchparam;
                }
            }
        },
        proxy: {
            type: 'ajax',
            url: 'getUserListAction.action',
            reader: {
                type: 'json',
                root: 'items',
                totalProperty:'totalCount'
            }
        }
     });

 /**
  *grid表格头 
  */    
     var col_ry=[
        {xtype:'rownumberer'}, 
        {dataIndex:'flogname',text:'登陆名称',width:100,menuDisabled:true,sortable:false},
        {dataIndex:'fusername',text:'用户名称',width:100,menuDisabled:true,sortable:false},
        {dataIndex:'fpassword',text:'用户密码',width:100,menuDisabled:true,sortable:false},
        {dataIndex:'fpower',text:'用户角色',width:100,menuDisabled:true,sortable:false},
        {dataIndex:'swjg_dm',text:'税务机关代码',width:200,menuDisabled:true,sortable:false},
        {dataIndex:'swjg_mc',text:'税务机关名称',width:200,menuDisabled:true,sortable:false}
     ];

     Ext.onReady(function(){
         Ext.create('Ext.panel.Panel',{
             id:'main-panel',
             width:Ext.getBody().getWidth()-20,
             renderTo:Ext.getBody(),
             layout:{
                 type:'vbox',
                 align: 'stretch'
             },
             items:[{
                        xtype: 'grid',
                        id:'grid_ry',
                        title:'人员信息表',
                        height:650,
                        //selModel: Ext.create('Ext.selection.CheckboxModel', { mode: "SIMPLE" }),
                        columns: col_ry,            
                        store: store_ry,
                        dockedItems:[
                            {
                                xtype: 'toolbar',
                                items:[{
                                    xtype:'button',
                                    text:'查询',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:showSearchDlg
                                },{
                                    xtype:'button',
                                    text:'新增',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:showNewDlg
                                },{
                                    xtype:'button',
                                    text:'修改',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:function(){
                                        var g=Ext.getCmp('grid_ry');
                                        var m=g.getSelectionModel().getSelection();
                                        if (m.length>0) {
                                            showModifDlg(m[0].raw);
                                        }else{
                                            Ext.Msg.alert('提示信息','请选中要修改的人员！');
                                        };
                                    }
                                },{
                                    xtype:'button',
                                    text:'删除',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:function(){
                                        var g=Ext.getCmp('grid_ry');
                                        var m=g.getSelectionModel().getSelection();
                                        if (m.length>0) {
                                            showDelDlg(m[0].raw);
                                        }else{
                                            Ext.Msg.alert('提示信息','请选中要删除的人员！');
                                        };
                                    }
                                }]
                            },{
                                xtype: 'pagingtoolbar',
                                store: store_ry,   
                                dock: 'bottom',
                                displayInfo: true
                            }
                        ], 
                        flex: 1  
             }]
         });
     });

     function showSearchDlg(){
         var dlg=Ext.create('Ext.window.Window',{
             title:'查询条件',
             width:350,
             height:300,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'textfield',
                                fieldLabel:'登陆名称',
                                name: 'flogname',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'textfield',
                                fieldLabel:'用户名称',
                                name: 'fusername',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'treepicker',
                                fieldLabel:'税务机关',
                                name: 'swjg_dm',
                                valueField : 'id',
                                displayField : 'text',
                                store : new Ext.data.TreeStore({
                                    root : {
                                        expanded : true
                                    },
                                    proxy : {
                                        type : 'ajax',
                                        url : 'getOrgListAction.action'
                                    },
                                    autoLoad : true
                                }),
                                labelWidth:80,
                                width:300
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    searchparam=this.up('form').getForm().getValues();
                                    store_ry.loadPage(1);
                                    dlg.close();
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }
     function showNewDlg(){
         var dlg=Ext.create('Ext.window.Window',{
             title:'新增人员信息',
             width:350,
             height:300,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'textfield',
                                fieldLabel:'登陆名称',
                                name: 'flogname',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'textfield',
                                fieldLabel:'用户名称',
                                name: 'fusername',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'treecombobox',
                                fieldLabel:'税务机关',
                                name: 'swjg_dm',
                                valueField:'id',
                                url: 'getOrgListAction.action',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'combo',
                                fieldLabel: '用户角色',
                                labelWidth:80,
                                width:300,
                                name: 'fpower',
                                mode:'local',
                                editable:false,
                                allowBlank: false,
                                displayField:'text',
                                valueField:'id',
                                store: Ext.create('Ext.data.Store', {
                                            fields : ['id', 'text'],
                                            data:[{'id':'0','text':'查询员'},{'id':'1','text':'管理员'}]
                                })
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    var f=this.up('form').getForm();
                                    if (f.isValid()) {
                                        f.submit({
                                            url:'addUserAction.action',
                                            clientValidation:false,
                                            waitMsg:'正在提交数据,请稍候...',
                                            success: function(form, action){
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                                store_ry.loadPage(1);
                                                dlg.close();
                                            },
                                            failure: function(form, action) {
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                            }
                                        });
                                    };
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }
     function showModifDlg(raw){
         var dlg=Ext.create('Ext.window.Window',{
             title:'修改人员信息',
             width:350,
             height:300,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'textfield',
                                fieldLabel:'登陆名称',
                                name: 'flogname',
                                value:raw.flogname,
                                readOnly:true,
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'textfield',
                                fieldLabel:'用户名称',
                                name: 'fusername',
                                value:raw.fusername,
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'treecombobox',
                                fieldLabel:'税务机关',
                                name: 'swjg_dm',
                                value:raw.swjg_dm,
                                valueField:'id',
                                url: 'getOrgListAction.action',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'combo',
                                fieldLabel: '用户角色',
                                labelWidth:80,
                                width:300,
                                name: 'fpower',
                                value:raw.fpower,
                                mode:'local',
                                editable:false,
                                allowBlank: false,
                                displayField:'text',
                                valueField:'id',
                                store: Ext.create('Ext.data.Store', {
                                            fields : ['id', 'text'],
                                            data:[{'id':'0','text':'查询员'},{'id':'1','text':'管理员'}]
                                })
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    var f=this.up('form').getForm();
                                    if (f.isValid()) {
                                        f.submit({
                                            url:'modifUserAction.action',
                                            clientValidation:false,
                                            waitMsg:'正在提交数据,请稍候...',
                                            success: function(form, action){
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                                store_ry.loadPage(1);
                                                dlg.close();
                                            },
                                            failure: function(form, action) {
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                            }
                                        });
                                    };
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }
     function showDelDlg(raw){
         var dlg=Ext.create('Ext.window.Window',{
             title:'删除人员信息',
             width:350,
             height:300,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'textfield',
                                fieldLabel:'登陆名称',
                                name: 'flogname',
                                value:raw.flogname,
                                readOnly:true,
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'textfield',
                                fieldLabel:'用户名称',
                                name: 'fusername',
                                value:raw.fusername,
                                readOnly:true,
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'treecombobox',
                                fieldLabel:'税务机关',
                                name: 'swjg_dm',
                                value:raw.swjg_dm,
                                readOnly:true,
                                valueField:'id',
                                url: 'getOrgListAction.action',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'combo',
                                fieldLabel: '用户角色',
                                labelWidth:80,
                                width:300,
                                name: 'fpower',
                                value:raw.fpower,
                                readOnly:true,
                                mode:'local',
                                editable:false,
                                allowBlank: false,
                                displayField:'text',
                                valueField:'id',
                                store: Ext.create('Ext.data.Store', {
                                            fields : ['id', 'text'],
                                            data:[{'id':'0','text':'查询员'},{'id':'1','text':'管理员'}]
                                })
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    var f=this.up('form').getForm();
                                    if (f.isValid()) {
                                        f.submit({
                                            url:'delUserAction.action',
                                            clientValidation:false,
                                            waitMsg:'正在提交数据,请稍候...',
                                            success: function(form, action){
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                                store_ry.loadPage(1);
                                                dlg.close();
                                            },
                                            failure: function(form, action) {
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                            }
                                        });
                                    };
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }